<?php 
    namespace Media\Controller;
    
    use Zend\Mvc\Controller\AbstractActionController;
    use Zend\View\Model\ViewModel;
    use Zend\View\Model\JsonModel;
    use Application\Entity\LoadFacebook;
    use Media\Entity\Album;
    use Media\Entity\Photo;
    use Application\Entity\Provider;
    use Application\Component\CommentComponent;
    use Activity\Entity\AlbumActivity;
    use Activity\Entity\PhotoActivity;

class FacebookAlbumController extends AbstractActionController{
        
        protected $em;
        
        protected $entityComponent;
        protected $facebookComponent;
        protected $facebookLoadComponent;
        protected $providerComponent;
        protected $markAlbumComponent;
        protected $commentComponent;
        protected $user;
        protected $nbMax = 20;
        
        protected $activityComponent;
        //protected 
        
        public function init(){
            $this->em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
            
            $this->entityComponent = $this->getServiceLocator()->get('AlbumComponent');
            $this->facebookComponent =  $this->getServiceLocator()->get('ReverseOAuth2\Facebook');// $this->getServiceLocator()->get('FacebookComponent');
            $this->loadFacebookComponent = $this->getServiceLocator()->get('LoadFacebookComponent');
            $this->providerComponent = $this->getServiceLocator()->get('ProviderComponent');
            $this->markAlbumComponent = $this->getServiceLocator()->get('MarkAlbumComponent');

            $this->activityComponent = $this->getServiceLocator()->get('ActivityComponent');
            $this->commentComponent = new CommentComponent($this->em);
            $this->user = $this->identity();
        }
        
        public function checkNotImportedAction(){
            $this->init();
            $ids = $this->params()->fromRoute('id', "");//Facebook ids
            $result = array();
            try{
                $ids_array = explode("-", $ids);
                if(($this->user!=null)&&($ids_array!=null)&&(count($ids_array)>0)){
                    $albums_found = $this->entityComponent->listFacebookAlbums($this->user->uid, $ids_array);
                    $correspondance = array();
                    $albums = array();
                    foreach ($ids_array as $id_facebook){
                        $correspondance[$id_facebook] = "0";
                    }
                    foreach ($albums_found as $album){
                        $correspondance[$album->source_album_facebook_id] = $album->id;
                    }
                    foreach ($correspondance as $key => $value){
                        $albums[] = array(
                        	"facebook_id"=> $key,
                            "id"=>$value
                        );
                    }
                    $result["albums"] = $albums;
                }
            }catch(\Exception $e){
                
            }
            
            return new JsonModel($result);
        }
        public function createAlbumFacebookAction(){
            $this->init();
            $result = array();
            if($this->user==null) return new JsonModel($result);
            try{
                $provider = $this->providerComponent->get(Provider::$FACEBOOK_PROVIDER_ID);
                
                $name = $this->getRequest()->getPost('name', "");
                $id = $this->getRequest()->getPost('id', "");
                $path = $this->getRequest()->getPost("path", null);
                $description = $this->getRequest()->getPost('description', null);
                $type = $this->getRequest()->getPost('type', 1);
                $native = $this->getRequest()->getPost('native', 1);
                $album = new Album();
                //try {
                $album->name = $name;
                $album->path = $path;
                $album->description = $description;
                $album->type = $type;
                $album->native = $native;
                $album->provider_id = $provider;
                $album->user_uid = $this->user;
                $album->source_album_facebook_id = $id;
                $this->entityComponent->insert($album);
                 
                if($album!=null && $album->id!=null){
                	$albumActivity = new AlbumActivity();
                	$albumActivity->album = $album;
                	$albumActivity->date = $album->created_at;
                	$albumActivity->user = $album->user_uid;
                	$this->activityComponent->insert($albumActivity);//Saving the photoActivity
                }
                
                $result = array(
                    "result_code"=>"success",
                	'success' => 'User album created',
                	'album'=>$album->getJson(),
                );
            }catch(\Exception $e){
                $result = array(
                    "result_code"=>"exception",
                    'exception'=>$e->getMessage(),
                );
            }
            
            return new JsonModel($result);
        }
        
        public function importPhotosFacebookAction(){
            $this->init();
            $result = array();
            if($this->user==null) return new JsonModel($result);
            try{
                $id = (int)$this->getRequest()->getPost('id', 0);
                $nb_photos = (int)$this->getRequest()->getPost('nb_photos', 0);
                $album = $this->entityComponent->get($id);
                $nb = 0;
                if($album!=null && $nb_photos>0){
                    $provider = $this->providerComponent->get(Provider::$FACEBOOK_PROVIDER_ID);
                    for($i=1; $i<=$nb_photos; $i++){
                        $id_facebook = $this->getRequest()->getPost('id_facebook'.$i, "");
                        $name = $this->getRequest()->getPost('name'.$i, "");
                        $description = $this->getRequest()->getPost('description'.$i, null);
                        $path = $this->getRequest()->getPost("path".$i, null);
                        $photo = new Photo();
                        $photo->name = ($name==null)?$name:$album->name;
                        $photo->path = $path;
                        $photo->source_album_facebook_id = $id_facebook;
                        $photo->description = ($description==null)?$album->description:$description;
                        $photo->type = "";
                        $photo->user_uid = $this->user;
                        $photo->album = $album;
                        $photo->provider_id = $provider;
                        $this->entityComponent->insert($photo);
                        
                        $nb++;
                        
                        if($photo!=null && $photo->id!=null){
                            if($album->path==$id_facebook){
                            	$album->path = $path;
                            	$this->entityComponent->update($album);
                            }
                        	$photoActivity = new PhotoActivity();
                        	$photoActivity->photo = $photo;
                        	$photoActivity->date = $photo->created_at;
                        	$photoActivity->user = $photo->user_uid;
                        	$this->activityComponent->insert($photoActivity);//Saving the photoActivity
                        }
                    }
                }
                
                $result = array(
                    "result_code"=>"success",
                    'success' => 'Photos imported',
                    "nb"=>$nb,
                	'album'=>$album->getJson(),
                );
            }catch(\Exception $e){
                $result = array(
                    "result_code"=>"exception",
                    "nb"=>$nb,
                	'exception'=>$e->getMessage(),
                );
            }
            
            return new JsonModel($result);
        }





    }
